home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
LIFER__
/
PROTO
/
P
/
MAIN_LIF.C
next >
Wrap
C/C++ Source or Header
|
1991-07-23
|
22KB
|
554 lines
/* Main_Life */
/* Program name: Main_Life.c */
/* Function: This is the main module for this program. */
/* History: 7/23/91 Original by Prototyper 3.0 */
#include "PCommonLife.h" /* Common */
#include "Common_Life.h" /* Common */
#include "PUtils_Life.h" /* General Utilities */
#include "Utils_Life.h" /* General Utilities */
#include "InitExitLife.h" /* Init, Exit handlers */
#include "EventsLife.h" /* Extra event handlers */
#include "PA_Life_Alert.h" /* Alert */
#include "PD_LIFE_INPUT.h" /* Modal Dialog */
#include "PMD_LIFE_WINDOW.h" /* Modeless Dialog */
#include "PIMenu_Life.h" /* Init menus */
#include "PDoMenuLife.h" /* Handle menus */
Boolean DoIt; /* Flag saying an event is ready */
short code; /* Determine event type */
WindowPtr whichWindow; /* See which window for event */
long mResult; /* Menu list and item selected values */
short theMenu,theItem; /* Menu list and item selected */
Boolean Is_A_Dialog; /* Flag for modless dialogs */
short charCode, itemHit; /* For modeless dialogs*/
char ch; /* Key pressed in Ascii */
Boolean DoTheModelessEvent, CmdDown; /* For modeless dialogs*/
WindowPeek thePeeked; /* For modeless dialogs*/
/* Prototypes */
/* See if WaitNextEvent is available */
static Boolean WNEIsImplemented(void);
/* Check for user events */
static void Handle_User_Event(void);
/* Handle key strokes */
static void DoKeyEvent(void);
/* Handle a diskette inserted */
static void DoDiskEvent(void);
/* Handle a window being resized */
static void DoGrow(WindowPtr whichWindow);
/* Handle a window being dragged */
static void DoDrag(WindowPtr whichWindow);
/* Handle a window goaway box */
static void DoGoAway(WindowPtr whichWindow);
/* Handle a hit in the window */
static void DoInContent(WindowPtr whichWindow);
/* Handle an update to the window */
static void DoUpdate(void);
/* Handle an activate of the window */
static void DoActivate(void);
/* MAIN entry point */
void main(void);
/* ======================================================= */
/* Routine: WNEIsImplemented */
/* Purpose: See if the MultiFinder trap, WaitNextEvent, is available */
Boolean WNEIsImplemented() /* See if WaitNextEvent is available */
{
#define WNETrapNumber 0xA860 /* The expected trap number */
SysEnvRec theWorld; /* Environment record */
OSErr discardError; /* Error code returned */
Boolean theWNEIsImplemented; /* Value to return */
HasColorQD = FALSE; /* Init to no color QuickDraw */
HasFPU = FALSE; /* Init to no floating point chip */
InTheForeground = TRUE; /* Init to a foreground app */
discardError = SysEnvirons(1, &theWorld); /* Check how old this system is */
if (theWorld.machineType < 0) /* Negative means really old */
theWNEIsImplemented = FALSE; /* Really old ROMs, no WNE possible */
else
{
theWNEIsImplemented = TrapAvailable(WNETrapNumber, ToolTrap);/* See if trap is there */
HasColorQD = theWorld.hasColorQD; /* Flag for Color QuickDraw being available */
HasFPU = theWorld.hasFPU; /* Flag for Floating Point Math Chip being available */
}
return(theWNEIsImplemented);
}
/* ======================================================= */
/* Routine: Handle_User_Event */
/* Purpose: Check for user events */
void Handle_User_Event() /* Check for user events */
{
UserEventRec TheUserEvent; /* The user event */
GetUserEvent(&TheUserEvent); /* Check for any user events */
if (TheUserEvent.ID != UserEvent_None) /* Only do if we have any */
{
switch (TheUserEvent.ID) /* Key off the Event ID */
{
case UserEvent_Open_Window: /* Open a Window or Modeless dialog */
switch (TheUserEvent.ID2) /* Do the appropiate window */
{
case Res_D_LIFE_INPUT:
PD_LIFE_INPUT(); /* Open this modal dialog */
break;
case Res_MD_LIFE_WINDOW:
Open_LIFE_WINDOW(); /* Open this modeless dialog */
break;
case Res_A_Life_Alert:
PA_Life_Alert(); /* Open this alert */
break;
default: /* Handle others */
break;
} /* End of the switch */
break;
case UserEvent_Close_Window: /* Close a Window or Modeless dialog */
switch (TheUserEvent.ID2) { /* Do the appropiate window */
case Res_MD_LIFE_WINDOW: Close_LIFE_WINDOW(WPtr_LIFE_WINDOW);
Close_LIFE_WINDOW(WPtr_LIFE_WINDOW);/* Close this modeless dialog */
break;
default: /* Handle others */
break;
} /* End of the switch */
break;
default: /* Not standard, must be program specific */
Handle_UserEvent(&TheUserEvent); /* Let program specific handle it */
break;
} /* End of switch */
} /* End of handling a user event */
}
/* ======================================================= */
/* Routine: DoKeyEvent */
/* Purpose: Handle a key pressed */
void DoKeyEvent() /* Handle key presses */
{
short charCode; /* Key code */
char ch; /* Key pressed in Ascii */
long mResult; /* Menu list and item, if a command key */
short theMenu,theItem; /* Menu list and item, if command key */
if (HandleKey(&myEvent)) /* Allow for special key handling */
{
charCode = myEvent.message & charCodeMask; /* Get the character */
ch = (char)charCode; /* Change it to ASCII */
if ((myEvent.modifiers / cmdKey) & 1) /* See if Command key is down */
{
mResult = MenuKey(ch); /* See if a menu selection */
theMenu = HiWord(mResult); /* Get the menu list number */
theItem = LoWord(mResult); /* Get the menu item number */
if (theMenu != 0) /* See if a list was selected */
Handle_My_Menu(theMenu, theItem); /* Do the menu selection */
if (((ch == 'x') || (ch == 'X')) && (theInput != NIL))/* See if a standard Cut */
TECut(theInput); /* Handle a Cut in a TE area */
if (((ch == 'c') || (ch == 'C')) && (theInput != NIL))/* See if a standard Copy */
TECopy(theInput); /* Handle a Copy in a TE area */
if (((ch == 'v') || (ch == 'V')) && (theInput != NIL))/* See if a standard Paste */
TEPaste(theInput); /* Handle a Paste in a TE area */
} /* End of Command key special condition */
else if (theInput != NIL)
TEKey(ch,theInput); /* Place the normal key stroke */
} /* End of Standard keystroke handling */
}
/* ======================================================= */
/* Routine: DoDiskEvent */
/* Purpose: Handle a diskette inserted */
void DoDiskEvent() /* Handle disk inserted */
{
short theError; /* Error returned from mount */
if (HandleDisk(&myEvent)) /* Allow for special disk inserted handling */
{
if (HiWord(myEvent.message) != noErr) /* See if a diskette mount error */
{ /* due to unformatted diskette inserted */
myEvent.where.h = ((screenBits.bounds.right - screenBits.bounds.left) / 2) - (304 / 2);/* Center horz */
myEvent.where.v = ((screenBits.bounds.bottom - screenBits.bounds.top) / 3) - (104 / 2);/* Top 3ed vertically */
InitCursor(); /* Make sure it has an arrow cursor */
theError = DIBadMount(myEvent.where, myEvent.message);/* Let the OS handle the diskette */
}
} /* End of Standard disk inserted handling */
}
/* ======================================================= */
/* Routine: DoGrow */
/* Purpose: Handle a window resize */
void DoGrow(whichWindow) /* Handle a window being resized */
WindowPtr whichWindow;
{
Rect OldRect; /* Window rect before the grow */
Point myPt; /* Point for tracking grow box */
Rect GrowRect; /* Set the grow bounds */
long mResult; /* Result from the grow */
if (whichWindow != NIL) /* See if we have a legal window */
{
SetPort(whichWindow); /* Get ready to draw in this window */
myPt = myEvent.where; /* Get mouse position */
GlobalToLocal(&myPt); /* Make it relative */
OldRect = whichWindow->portRect; /* Save the rect before resizing */
SetRect(&GrowRect, 4, 4, (screenBits.bounds.right - screenBits.bounds.left)-4, (screenBits.bounds.bottom - screenBits.bounds.top) - 4);/* l,t,r,b */
mResult = GrowWindow(whichWindow, myEvent.where, &GrowRect);/* Grow it */
SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), TRUE);/* Resize to result */
SetPort(whichWindow); /* Get ready to draw in this window */
myPt.h = whichWindow->portRect.right - whichWindow->portRect.left; /* Local right edge */
myPt.v = whichWindow->portRect.bottom - whichWindow->portRect.top; /* Local bottom edge */
SetRect(&GrowRect, 0, myPt.v - 15, myPt.h + 15, myPt.v + 15); /* Position for horz scrollbar area */
EraseRect(&GrowRect); /* Erase old area */
InvalRect(&GrowRect); /* Flag us to update it */
SetRect(&GrowRect, myPt.h - 15, 0, myPt.h + 15, myPt.v + 15); /* Position for vert scrollbar area */
EraseRect(&GrowRect); /* Erase old area */
InvalRect(&GrowRect); /* Flag us to update it */
DrawGrowIcon(whichWindow); /* Draw the grow Icon again */
} /* End of (WhichWindow <> nil) */
}
/* ======================================================= */
/* Routine: DoDrag */
/* Purpose: Drag a window around */
void DoDrag( whichWindow) /* Handle a window being dragged */
WindowPtr whichWindow;
{
Rect OldRect; /* Window rect before the drag */
Rect tempRect; /* temporary rect */
OldRect = whichWindow->portRect; /* Save the rect before resizing */
tempRect = screenBits.bounds; /* Get screen area, l,t,r,b, drag area */
SetRect(&tempRect,tempRect.left+4,tempRect.top+4,tempRect.right-4,tempRect.bottom - 4);
DragWindow(whichWindow, myEvent.where, &tempRect);/* Drag the window */
switch (GetWRefCon(whichWindow)) /* Do the appropiate window */
{
case Res_MD_LIFE_WINDOW:
Moved_LIFE_WINDOW(&OldRect, whichWindow);/* Moved this modeless dialog */
break;
default: /* Handle others */
break;
} /* End of the switch */
}
/* ======================================================= */
/* Routine: DoGoAway */
/* Purpose: Close a window */
void DoGoAway( whichWindow) /* Handle a window goaway box */
WindowPtr whichWindow;
{
Rect OldRect; /* Window rect before the drag */
Rect tempRect; /* temporary rect */
if (TrackGoAway(whichWindow,myEvent.where) == TRUE)/* See if mouse released in GoAway box */
{ /* Handle the GoAway */
switch (GetWRefCon(whichWindow)) /* Do the appropiate window */
{
case Res_MD_LIFE_WINDOW:
Close_LIFE_WINDOW(whichWindow);/* Close this modeless dialog */
break;
default: /* Handle others */
break;
} /* End of the switch */
} /* End of TrackGoAway */
}
/* ======================================================= */
/* Routine: DoInContent */
/* Purpose: Pressed in the content area */
void DoInContent( whichWindow) /* Handle a hit in the window */
WindowPtr whichWindow;
{
if (whichWindow != FrontWindow()) /* See if already selected or not, in front if selected */
SelectWindow(whichWindow); /* Select this window to make it active */
else /* If already in front the already selected */
{ /* Handle the press in the content */
SetPort(whichWindow); /* Get ready to draw in this window */
} /* End of else */
}
/* ======================================================= */
/* Routine: DoUpdate */
/* Purpose: Got an update event */
void DoUpdate() /* Handle an update to the window */
{
WindowPtr whichWindow; /* See which window for event */
whichWindow = (WindowPtr)myEvent.message; /* Get the window the update is for */
BeginUpdate(whichWindow); /* Set the clipping to the update area */
EndUpdate(whichWindow); /* Return to normal clipping area */
}
/* ======================================================= */
/* Routine: DoActivate */
/* Purpose: Got an activate or deactivate event */
void DoActivate() /* Handle an activate of the window */
{
Boolean Do_An_Activate; /* Flag to pass */
WindowPtr whichWindow; /* See which window for event */
whichWindow = (WindowPtr)myEvent.message; /* Get the window the update is for */
Do_An_Activate = ((myEvent.modifiers & 0x0001) != 0);/* Make sure it is Activate and not DeActivate */
}
/* ======================================================= */
void main() /* Start of main body */
{
MoreMasters(); /* This reserves space for more handles */
MaxApplZone(); /* Give us room for memory allocation */
InitGraf(&thePort); /* Quickdraw Init */
InitFonts(); /* Font manager init */
InitWindows(); /* Window manager init */
InitMenus(); /* Menu manager init */
TEInit(); /* Text edit init */
InitDialogs(NIL); /* Dialog manager */
FlushEvents ( everyEvent , 0 ); /* Clear out all events */
InitCursor(); /* Make an arrow cursor */
doneFlag = FALSE; /* Do not exit program yet */
Init_My_Menus(); /* Initialize menu bar */
theInput = NIL; /* Init to no text edit selection active */
SleepValue = 40; /* Set sleep value */
WNE = WNEIsImplemented(); /* See if WaitNextEvent is available */
UserEventList = NIL; /* No user events yet */
ApplInit_Life(); /* Handle extra program initialization */
I_PD_LIFE_INPUT(); /* Initialize the modal dialog globals */
Init_LIFE_WINDOW(); /* Initialize the modeless dialog routines */
I_PA_Life_Alert(); /* Initialize the alert globals */
PD_LIFE_INPUT(); /* Open the modal dialog routines at program start */
do /* Start of main event loop */
{
ApplLoop_Life(); /* Let into main loop */
Handle_User_Event(); /* Check for user events */
if (theInput != NIL) /* See if a TE is active */
TEIdle(theInput); /* Blink the cursor if everything is ok */
if (WNE == TRUE) /* See if do the MultiFinder way */
DoIt = WaitNextEvent(everyEvent, &myEvent, SleepValue, NIL);/* Wait for an event */
else
{
SystemTask(); /* For support of desk accessories */
DoIt = GetNextEvent(everyEvent, &myEvent);/* See if an event is ready */
}
ApplEvent_Life(&DoIt,&myEvent); /* Let us at the event first */
if (DoIt == TRUE) /* If event then... */
{ /* Start handling the event */
Is_A_Dialog = IsDialogEvent(&myEvent); /* See if a modeless dialog event */
if (Is_A_Dialog == TRUE) /* Handle a dialog event */
{
if (myEvent.what == updateEvt) /* Handle the update of a Modeless Dialog */
{
whichWindow = (WindowPtr)myEvent.message; /* Get the window the update is for */
BeginUpdate(whichWindow); /* Set update clipping area */
Update_LIFE_WINDOW(whichWindow); /* Update the modeless dialog */
EndUpdate(whichWindow); /* Return to normal clipping area */
}
else
{
DoTheModelessEvent = TRUE; /* Go ahead and do it so far */
if (myEvent.what == keyDown) /* Check the key down, for a command key event */
{
CmdDown = ((myEvent.modifiers / cmdKey) & 1);/* Get the command key state */
charCode = myEvent.message & charCodeMask;/* Get the character */
ch = (char)charCode; /* Change it to ASCII */
if ((charCode == 13) || (charCode == 0x03))/* CR or Enter */
DoTheModelessEvent = TRUE; /* Handle the default selection */
if (CmdDown != 0) /* Handle if the command key was down */
{
mResult = MenuKey(ch); /* See if a menu selection */
theMenu = HiWord(mResult); /* Get the menu list number */
theItem = LoWord(mResult); /* Get the menu item number */
if (theMenu != 0) /* See if a list was selected */
Handle_My_Menu(theMenu, theItem); /* Do the menu selection */
whichWindow = FrontWindow(); /* Get the front window */
if ((ch == 'x') || (ch == 'X')) /* Handle a CUT */
DlgCut(whichWindow); /* Do the dialog cut */
if ((ch == 'c') || (ch == 'C')) /* Handle a COPY */
DlgCopy(whichWindow); /* Do the dialog copy */
if ((ch == 'v') || (ch == 'V')) /* Handle a PASTE */
DlgPaste(whichWindow); /* Do the dialog paste */
DoTheModelessEvent = FALSE; /* We handled the command key */
}
}
if (DoTheModelessEvent == TRUE) /* Do we handle it? */
{
if ((DialogSelect(&myEvent, &whichWindow, &itemHit)) || (myEvent.what == mouseDown) || (myEvent.what == keyDown)) /* Ck if do it */
{
Do_LIFE_WINDOW(&myEvent,whichWindow,itemHit);/* Handle the Modeless Dialog */
}
}
} /* End of not update event */
} /* End of Is_A_Dialog */
else /* Otherwise handle a window */
{
switch (myEvent.what) /* Decide type of event */
{
case mouseDown : /* Mouse button pressed */
code = FindWindow(myEvent.where, &whichWindow);/* Get which window the event happened in */
switch (code) /* Decide type of event again */
{
case inMenuBar : /* In the menubar */
mResult = MenuSelect(myEvent.where);/* Do menu selection */
theMenu = HiWord(mResult); /* Get the menu list number */
theItem = LoWord(mResult); /* Get the menu list item number */
Handle_My_Menu( theMenu, theItem); /* Handle the menu */
break; /* End of inMenuBar */
case inDrag : /* In window drag area */
DoDrag(whichWindow); /* Go drag the window */
break; /* End of InDrag */
case inGrow : /* In window grow area */
DoGrow(whichWindow); /* Handle the growing */
break; /* End of inGrow */
case inGoAway : /* In window goaway area */
DoGoAway(whichWindow);/* Handle the goaway button */
break; /* End of inGoAway */
case inContent : /* In window contents */
DoInContent(whichWindow);/* Handle the hit inside a window */
break; /* End of inContent */
case inSysWindow : /* See if a DA selectio */
SystemClick(&myEvent, whichWindow);/* Let other programs in */
break; /* End of inSysWindow */
default: /* Handle others */
break; /* End of otherwise */
} /* End of code case */
break; /* End of MouseDown */
case keyDown: /* Handle key inputs */
case autoKey: /* and auto repeats */
DoKeyEvent(); /* Get the key and handle it */
break; /* End of otherwise */
case updateEvt : /* Update event for a window */
DoUpdate(); /* Handle the update */
break; /* End of otherwise */
case diskEvt : /* Disk inserted event */
DoDiskEvent(); /* Handle a disk event */
break; /* End of otherwise */
case activateEvt : /* Window activated event */
DoActivate(); /* Handle the activation */
break; /* End of otherwise */
default: /* Used for debugging, to see what other events are coming in */
break; /* End of otherwise */
} /* End of case */
} /* End for not a modeless dialog event */
} /* end of GetNextEvent */
else /* Blink cursor in modeless TEs */
{
whichWindow = FrontWindow(); /* Get the current front window */
if (whichWindow != NIL) /* See if we have a window */
{
thePeeked = (WindowPeek)whichWindow;/* Peek inside, look for dialog */
if (thePeeked->windowKind == dialogKind)/* DialogSelect will crash if no dialogs */
{
if (DialogSelect(&myEvent, &whichWindow, &itemHit))/* Blink cursor in modeless TEs */
{
}
}
}
}
} /* end of while */
while (doneFlag == FALSE); /* End of the event loop */
ApplExit_Life(); /* Handle extra program termination code */
} /* end of main */